//
//  RegViewController.m
//  ForYourBeauty
//
//  Created by Kelly xu on 2019/5/27.
//  Copyright © 2019 changrenTech. All rights reserved.
//

#import "RegViewController.h"
#import "BindDeviceViewController.h"
#import "LoginViewModel.h"
#import "CountDownUtils.h"
@interface RegViewController (){
    
}
@property(nonatomic,weak)IBOutlet UIImageView *inputTextBg;
@property(nonatomic,weak)IBOutlet UIButton *regBtn;
@property(nonatomic,weak)IBOutlet UIButton *checkCodeBtn;
@property(nonatomic,weak)IBOutlet UITextField *phoneText;
@property(nonatomic,weak)IBOutlet UITextField *checkCodeText;
@property(nonatomic,weak)IBOutlet UITextField *pwdText;
@property(nonatomic,strong)LoginViewModel *loginViewModel;
@property(nonatomic,strong)dispatch_source_t countDownTimer;
@end

@implementation RegViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"立即注册";
    [self configUI];
}
-(void)configUI{
    [self.inputTextBg setImage:[[UIImage imageNamed:@"cornerBg"] stretchableImageWithLeftCapWidth:[UIImage imageNamed:@"cornerBg"].size.width/2 topCapHeight:[UIImage imageNamed:@"cornerBg"].size.height/2]];
    self.regBtn.layer.cornerRadius = 20;
    self.checkCodeBtn.layer.cornerRadius = 15;
}
-(IBAction)checkCodeBtnPressed:(id)sender{
    if (EC_ISNullStr(self.phoneText.text)) {
        Toast(@"请输入手机");
        return;
    }
    [CountDownUtils checkCodeWithBtn:self.checkCodeBtn buttonColor:[UIColor whiteColor] buttonHighlightedColor:[UIColor whiteColor] timer:self.countDownTimer handle:^{
        [self.loginViewModel fetchCheckCode:self.phoneText.text cmp:^{
            Toast(@"发送验证码成功");
        }];
    }];
}
-(IBAction)regBtnPressed:(id)sender{
    if (EC_ISNullStr(self.phoneText.text)) {
        Toast(@"请输入手机号");
        return;
    }
    if (EC_ISNullStr(self.checkCodeText.text)) {
        Toast(@"请输入验证码");
        return;
    }
    if (EC_ISNullStr(self.pwdText.text)) {
        Toast(@"请设置登录密码");
        return;
    }
    [self.loginViewModel registerWithMob:self.phoneText.text checkCode:self.checkCodeText.text pwd:self.pwdText.text cmp:^{
        Toast(@"注册成功");
        [self.rt_navigationController popViewControllerAnimated:YES complete:nil];
    }];
//    BindDeviceViewController *bindDevice = [[BindDeviceViewController alloc]init];
//    [self.rt_navigationController pushViewController:bindDevice animated:YES complete:nil];
}
#pragma mark - instance
-(LoginViewModel*)loginViewModel{
    if (!_loginViewModel) {
        _loginViewModel = [[LoginViewModel alloc]init];
    }
    return _loginViewModel;
}
#pragma mark - 倒计时
-(dispatch_source_t)countDownTimer{
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    _countDownTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    return _countDownTimer;
}
-(void)stopCountDownTimer{
    dispatch_source_cancel(self.countDownTimer);
    self.countDownTimer = nil;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
